#include "qresourcefile.h"

#include <QFile>

using namespace SHAREDLIB;

QResourceInfo::QResourceInfo()
{
}

QResourceInfo::~QResourceInfo()
{
}

void QResourceInfo::setResourceName(const QString &name)
{
    m_resourceName=name;
}

void QResourceInfo::setResourceData(const QByteArray &data)
{
    m_resourceData=data;
}

QString QResourceInfo::resourceName()
{
    return m_resourceName;
}

QByteArray QResourceInfo::resourceData()
{
    return m_resourceData;
}

void QResourceFile::clear()
{
    qDeleteAll(m_resourceInfos);
    m_resourceInfos.clear();
}

int QResourceFile::resourceCount()
{
    return m_resourceInfos.size();
}

QResourceFile::~QResourceFile()
{
    clear();
}

QResourceInfo *QResourceFile::resource(int index)
{
    if(index<0 || index>=m_resourceInfos.size())
    {
        return NULL;
    }
    else
    {
        return m_resourceInfos.at(index);
    }
}

QResourceInfo* QResourceFile::resource(const QString &resourceName)
{
    foreach(QResourceInfo* info,m_resourceInfos)
    {
        if(info->resourceName()==resourceName)
        {
            return info;
        }
    }
    return NULL;
}

void QResourceFile::addResource(QResourceInfo *info)
{
    if(!m_resourceInfos.contains(info))
    {
        m_resourceInfos.append(info);
    }
}

void QResourceFile::addResource(const QString &name, const QByteArray &data)
{
    QResourceInfo *info=resource(name);
    if(info!=NULL)
    {
        info->setResourceData(data);
    }
    else
    {
        info=new QResourceInfo;
        info->setResourceName(name);
        info->setResourceData(data);
        m_resourceInfos.append(info);
    }
}

void QResourceFile::removeResource(int index)
{
    if(index>=0 && index<m_resourceInfos.size())
    {
        delete m_resourceInfos.at(index);
        m_resourceInfos.removeAt(index);
    }
}

void QResourceFile::removeResource(QResourceInfo *info)
{
    if(m_resourceInfos.contains(info))
    {
        m_resourceInfos.removeAll(info);
        delete info;
    }
}

void QResourceFile::removeResource(const QString &name)
{
    foreach(QResourceInfo* info,m_resourceInfos)
    {
        if(info->resourceName()==name)
        {
            m_resourceInfos.removeAll(info);
            delete info;
            return;
        }
    }
}

void QResourceFile::setFileName(const QString &name)
{
    m_fileName=name;
}

QString QResourceFile::fileName()
{
    return m_fileName;
}

bool QResourceFile::save(const QString &path)
{
    if(m_fileName=="")
    {
        return false;
    }

    QString fileName=path+"/"+m_fileName+".rus";

    QFile f(fileName);
    if(!f.open(QFile::WriteOnly))
    {
        return false;
    }

    f.resize(0);

    QString str;
    int temp;
    QByteArray data;
    foreach(QResourceInfo* info,m_resourceInfos)
    {
        str=info->resourceName();
        f.write(str.toLocal8Bit().data(),strlen(str.toLocal8Bit().data())+1);
        data=info->resourceData();
        temp=data.size();
        f.write((char*)&temp,4);
        f.write(data);
    }

    f.close();
    return true;
}

bool QResourceFile::load(const QString &path)
{
    if(m_fileName=="")
    {
        return false;
    }

    QString fileName=path+"/"+m_fileName+".rus";

    QFile f(fileName);
    if(!f.open(QFile::ReadOnly))
    {
        return false;
    }

    QByteArray array=f.readAll();

    int pt=0;
    int size=array.size();
    char* buffer=array.data();
    QString str;
    int resize;
    QByteArray data;
    while(pt<size)
    {
        str=QString(buffer+pt);
        pt+=(strlen(buffer+pt)+1);
        if(pt>=size)
        {
            break;
        }
        memcpy((char*)&resize,buffer+pt,4);
        pt+=4;
        if(pt+resize>size)
        {
            break;
        }
        data=array.mid(pt,resize);
        addResource(str,data);
        pt+=resize;
    }

    f.close();

    return true;
}

QStringList QResourceFile::resourceNames()
{
    QStringList list;
    foreach(QResourceInfo* info,m_resourceInfos)
    {
        list.append(info->resourceName());
    }
    return list;
}
